package com.sdlogan.pbudget;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
/**
 * Class that will create the Pie Charts
 * @author Sean
 *
 */
public class PieChart {
	double [] inputs;
	String [] labels;
	int[] colors;
	String type;
	String time;
	MoneyTable mt;
	List<dbPackage> source;
	String month;
	String year;
	Iterator it;
	
	/**
	 * Constructor
	 * @param time : Monthly or Yearly
	 * @param list : the Data
	 * @param type : Income or Expense
	 */
	public PieChart(String time,List<dbPackage> list,String type) {
		this.time = time;
		this.type = type;
		Calendar cal = Calendar.getInstance();
		year = Integer.toString(cal.get(Calendar.YEAR),10);
		Date date = new Date();
		month = new SimpleDateFormat("MMMM").format(date);
		source = list;
		Log.i("Sean","number of elements");
		Log.i("Sean","the type" + type);
		if (type.equals("Income")) {
			labels = new String[] {"Job","Bonus","Gift","Sale","Other"};
			colors = new int[] {Color.RED,Color.CYAN,Color.GREEN,Color.YELLOW,Color.BLUE};
			inputs = new double[] {0,0,0,0,0};
		}
		else {
			labels = new String[] {"Food","Bills","Savings","Entertainment","Transportation","Other"};
			colors = new int[] {Color.RED,Color.CYAN,Color.GREEN,Color.YELLOW,Color.BLUE,Color.WHITE};
			inputs = new double[] {0,0,0,0,0,0};
		}
			
		updateValues();
	}
	
	/**
	 * Update the array data so the data displayed will be correct
	 */
	private void updateValues() {
		if (type.equals("Income")) { 
			
				for (int i = 0; i < 5; i++) {
					it = source.iterator();
						while (it.hasNext()) {
							dbPackage current = (dbPackage) it.next();
								if (current.category.equals("Job") && current.type.equals(type) && i==0) {
										if (time.equals("Monthly")) {
											inputs[i] += current.monthlyvalue;
										}
										else {
											inputs[i] += current.yearlyvalue;
										}
								}
								else if (current.category.equals("Bonus") && current.type.equals(type) && i==1) {
									if (time.equals("Monthly")) {
										inputs[i] += current.monthlyvalue;
									}
									else {
										inputs[i] += current.yearlyvalue;
									}
								}
								else if (current.category.equals("Gift") && current.type.equals(type) && i==2) {
									if (time.equals("Monthly")) {
										inputs[i] += current.monthlyvalue;
									}
									else {
										inputs[i] += current.yearlyvalue;
									}
								}
								else if (current.category.equals("Sale") && current.type.equals(type) && i==3) {
									if (time.equals("Monthly")) {
										inputs[i] += current.monthlyvalue;
									}
									else {
										inputs[i] += current.yearlyvalue;
									}
								}
								else  if (current.category.equals("Other") && current.type.equals(type) && i==4){
									if (time.equals("Monthly")) {
										inputs[i] += current.monthlyvalue;
									}
									else {
										inputs[i] += current.yearlyvalue;
									}
								}
								else {
									//do nothing
								}
							}
				}
		}
		else {
			
				for (int j = 0; j < 6; j++) {
					it = source.iterator();
						while (it.hasNext()) {
							dbPackage current = (dbPackage) it.next();
							if (current.category.equals("Food") && current.type.equals(type) && j==0) {
								if (time.equals("Monthly")) {
									inputs[j] += current.monthlyvalue;
								}
								else {
									inputs[j] += current.yearlyvalue;
								}
							}
							else if (current.category.equals("Bills") && current.type.equals(type) && j==1) {
								if (time.equals("Monthly")) {
									inputs[j] += current.monthlyvalue;
								}
								else {
									inputs[j] += current.yearlyvalue;
								}
							}
							else if (current.category.equals("Savings") && current.type.equals(type) && j==2) {
								if (time.equals("Monthly")) {
									inputs[j] += current.monthlyvalue;
								}
								else {
									inputs[j] += current.yearlyvalue;
								}
							}
							else if (current.category.equals("Entertainment") && current.type.equals(type) && j==3) {
								if (time.equals("Monthly")) {
									inputs[j] += current.monthlyvalue;
								}
								else {
									inputs[j] += current.yearlyvalue;
								}
							}
							else  if (current.category.equals("Transportation") && current.type.equals(type) && j==4){
								if (time.equals("Monthly")) {
									inputs[j] += current.monthlyvalue;
								}
								else {
									inputs[j] += current.yearlyvalue;
								}
							}
							else if (current.category.equals("Other") && current.type.equals(type) && j==5) {
								if (time.equals("Monthly")) {
									inputs[j] += current.monthlyvalue;
								}
								else {
									inputs[j] += current.yearlyvalue;
								}
							}
							else {
								//do nothing
							}
					}
				}
		}
	}
	
	/**
	 * Return the view of the graph
	 * @param ctx : The context of the graph activity
	 * @return    : The view of the graph
	 */
	public View getView(Context ctx) 
	{
		
		//double[] values = {1,2,3,4,5};
		CategorySeries cs = new CategorySeries("Pie Chart");
		for (int i = 0; i < inputs.length; i++) {
			cs.add(labels[i], inputs[i]);
		}
		
		
		DefaultRenderer dr = new DefaultRenderer();
		for (int count = 0; count < colors.length; count++) {
			SimpleSeriesRenderer ssr = new SimpleSeriesRenderer();
			ssr.setColor(colors[count]);
			dr.addSeriesRenderer(ssr);
		}
		dr.setChartTitle(type + " Values");
		
		View v = ChartFactory.getPieChartView(ctx, cs, dr);
		return v;
	}
	
}
